package constant.milk.toilet.timer;

import java.util.Calendar;

import constant.milk.toilet.R;
import constant.milk.toilet.broadcast.TimerBroadcast;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;

public class TimerManager
{
	public TimerManager()
	{
	}

	public void startTimer(Context context)
	{
//		if (!isServiceRunning("constant.milk.toilet.timer.TimerService", context))
//		{
//			Intent serivce = new Intent(context, TimerService.class);
//			context.startService(serivce);
//		}
		
		Intent intent = new Intent(context, TimerService.class);
		PendingIntent sender = PendingIntent.getService(context, 0, intent,
				PendingIntent.FLAG_CANCEL_CURRENT);

		AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		manager.cancel(sender);
		
		manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, 1000, sender);

		startAlarm(context);
	}

	public void stopTimer(Context context)
	{
//		Intent service = new Intent(context, TimerService.class);
//		context.stopService(service);
		
		Intent intent = new Intent(context, TimerService.class);
		PendingIntent sender = PendingIntent.getService(context, 0, intent,
				PendingIntent.FLAG_CANCEL_CURRENT);

		AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		manager.cancel(sender);
		sender.cancel();
		
		stopAlarm(context);
	}

	public Boolean isServiceRunning(String serviceName, Context context)
	{
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);

		for (RunningServiceInfo runningServiceInfo : activityManager.getRunningServices(Integer.MAX_VALUE))
		{
			if (serviceName.equals(runningServiceInfo.service.getClassName()))
			{
				return true;
			}
		}

		return false;
	}

	public void stopAlarm(Context context)
	{
		Intent intent1 = new Intent(context, TimerBroadcast.class);
		PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent1,
				PendingIntent.FLAG_CANCEL_CURRENT);

		AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		manager.cancel(sender);
		sender.cancel();
	}

	public void startAlarm(Context context)
	{
		Intent intent1 = new Intent(context, TimerBroadcast.class);
		PendingIntent sender1 = PendingIntent.getBroadcast(context, 0, intent1,
				PendingIntent.FLAG_NO_CREATE);

		if (sender1 != null)
		{
			return;
		}

		SharedPreferences p = context.getSharedPreferences("pref",
				Activity.MODE_PRIVATE);
		int alarm = p.getInt(context.getString(R.string.prefTimerAlarm), 5);

		if (alarm == 0)
		{
			return;
		}

		int hour = p.getInt(context.getString(R.string.prefHour), 0);
		int min = p.getInt(context.getString(R.string.prefMin), 0);
		int sec = p.getInt(context.getString(R.string.prefSec), 0);

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(0);
		cal.add(Calendar.HOUR_OF_DAY, hour);
		cal.add(Calendar.MINUTE, min);
		cal.add(Calendar.SECOND, sec);

		long termTime = cal.getTimeInMillis();

		long namugi = termTime % (alarm * 60000);

		long ingyuTime = (alarm * 60000) - namugi;

		Intent intent = new Intent(context, TimerBroadcast.class);
		PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent,
				PendingIntent.FLAG_CANCEL_CURRENT);

		AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		manager.cancel(sender);
		
		int version = Build.VERSION.SDK_INT;
		
		if (version >= 19)
		{
			manager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ingyuTime, sender);
		}
		else
		{
			manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ingyuTime, sender);
		}
	}
}
